<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>竞赛题目管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加题目</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="viewImage">
            <img src="{{d.icon}}" lay-event="viewImg" onclick="previewImg(this)">
        </script>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    let problemId = 0;

    function child(data) {
        problemId = data.id
        console.log(problemId)
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
            table.render({
                elem: '#currentTableId',
                url: '/contest/admin/problem/' + problemId,
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                title: '题目列表',
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: 'ID', sort: true},
                    {field: 'problemName', minWidth: 130, title: '题目名称', align: "center"},
                    {field: 'submitNum', width: 130, title: '提交次数', sort: true},
                    {field: 'solvedNum', width: 130, title: '通过次数', sort: true},
                    {title: '操作', minWidth: 250, toolbar: '#currentTableBar', align: "center"}
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['prev', 'page', 'next', 'skip', 'count']
                },
                skin: 'line'
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加题目',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../../page/table/add_contest_problem.html',
                        success: function (layero, index) {
                            var iframe = window['layui-layer-iframe' + index];
                            //调用子页面的全局函数，向子页面传递参数
                            iframe.child(problemId);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    layer.confirm("确认删除所选内容吗?", function (index) {
                        data.forEach(function (item) {
                            //向服务器发送删除指令
                            $.ajax({
                                url: "/problem/" + item.id,//请求地址
                                dataType: "json",//数据格式
                                type: "DELETE",//请求方式
                                async: false,//是否异步请求
                                success: function (data) {   //如何删除成功
                                    if (data.data) {//删除成功
                                        layer.msg(data.msg);
                                    } else {
                                        layer.msg(data.msg);
                                    }
                                },
                                error: function (data) {
                                    layer.msg("服务器异常，请联系管理员!");
                                }
                            });
                        });
                        table.reload('currentTableId');
                        layer.close(index);
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '添加题目',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table/add_contest_problem.html',
                        success: function (layero, index) {
                            var iframe = window['layui-layer-iframe' + index];
                            //调用子页面的全局函数，向子页面传递参数
                            iframe.child(data);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('确认删除行吗？', function (index) {
                        //删除对应行（tr）的DOM结构，并更新缓存
                        table.reload('currentTableId');
                        layer.close(index);
                        //向服务器发送删除指令
                        $.ajax({
                            url: "/problem/" + obj.data.id,//请求地址
                            dataType: "json",//数据格式
                            type: "DELETE",//请求方式
                            async: false,//是否异步请求
                            success: function (data) {   //如何删除成功
                                if (data.data) {//删除成功
                                    layer.msg(data.msg);
                                } else {
                                    layer.msg(data.msg);
                                }
                            },
                            error: function (data) {
                                layer.msg("服务器异常，请联系管理员!");
                            }
                        });

                    });
                } else if (obj.event === 'test_case') {
                    var index = layer.open({
                        title: '测试用例管理',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/test_case_manage.html',
                        success: function (layero, index) {
                            var iframe = window['layui-layer-iframe' + index];
                            //调用子页面的全局函数，向子页面传递参数
                            iframe.child(data);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                }
            });
        });
    }


</script>

</body>
</html>
